//
// Created by Admin on 2021/9/20.
//

#include "entry.h"

void student_rank_by_Score(pStudent students, int student_count, int subject_count){
    for(int i=0;i<student_count;i++){
        if (students[i].total_score==0){
            calc_student_total(students,student_count,subject_count);
            break;
        }
    }
    printf("按照总分排名输出名次表：\n");
    int *visited = calloc(student_count,sizeof(int));
    for(int i=0;i<student_count;i++){
        int max_score = 0;
        int max_pos = 0;
        for(int j=0;j<student_count;j++){
            if(!visited[j] && students[j].total_score > max_score){
                max_score = students[j].total_score;
                max_pos = j;
            }
        }
        visited[max_pos] = 1;
        printf("\t第%d名：%s, 总分%d\n",i+1,students[max_pos].name,students[max_pos].total_score);
    }
    free(visited);
}

void calc_student_rank_by_Score(pStudent students, int student_count, int subject_count){
    for(int i=0;i<student_count;i++){
        if (students[i].total_score==0){
            calc_student_total(students,student_count,subject_count);
            break;
        }
    }
    int *visited = calloc(student_count,sizeof(int));
    for(int i=0;i<student_count;i++){
        int max_score = 0;
        int max_pos = 0;
        for(int j=0;j<student_count;j++){
            if(visited[j]==0 && students[j].total_score > max_score){
                max_score = students[j].total_score;
                max_pos = j;
            }
        }
        visited[max_pos] = 1;
        students[max_pos].rank = i + 1;
    }
    free(visited);
}